package com.government.web.controller.deepseek;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.government.common.annotation.RepeatSubmit;
import com.government.common.core.domain.AjaxResult;
import com.government.deepseek.DeepSeekBO;
import com.government.wxpay.bo.PaymentBO;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import lombok.extern.slf4j.Slf4j;

import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/deepseek")
@Slf4j
public class DeepseekController {


    private static final String API_KEY = "sk-e5ebd877c4884c7f83cdbd43107b8b80"; // 替换为你的API密钥
    private static final String API_URL = "https://api.deepseek.com/v1/chat/completions"; // 确认API端点

    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * deepseek对话
     */
    @RepeatSubmit
    @PostMapping(value = "/completions")
    public AjaxResult completions(@RequestBody DeepSeekBO bo) throws Exception {
        String response = callDeepSeekApi(bo.getProblem());

        // 解析JSON响应
        JsonNode rootNode = mapper.readTree(response);
        String content = rootNode.path("choices")
                .path(0)
                .path("message")
                .path("content")
                .asText();
        return AjaxResult.success(content);
    }

    public static String callDeepSeekApi(String message) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        ObjectNode requestBody = mapper.createObjectNode();
        requestBody.put("model", "deepseek-chat");

        ObjectNode messageNode = mapper.createObjectNode();
        messageNode.put("role", "user");
        messageNode.put("content", message);
        requestBody.putArray("messages").add(messageNode);

        HttpPost httpPost = new HttpPost(API_URL);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
        httpPost.setHeader("Authorization", "Bearer " + API_KEY);
        httpPost.setEntity(new StringEntity(requestBody.toString(), StandardCharsets.UTF_8));

        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(httpPost)) {

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                return EntityUtils.toString(entity);
            }
            throw new IOException("API响应为空");
        }
    }

}
